package com.quantstack.llcommonlibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.quantstack.llcommonlibrary.R;
import com.quantstack.llcommonlibrary.event.OnClickEvent;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/2/23 18:17
 * @Notes
 */
public class CustomTitle extends LinearLayout {
    private Context mContext;
    private TextView tvTitle;
    private ImageView ivBack;
    private ImageView ivRightIcon;
    public CustomTitle(Context context) {
        this(context,null);
    }

    public CustomTitle(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CustomTitle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(final Context context, AttributeSet attrs) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.layout_custom_title,this);
        tvTitle = findViewById(R.id.tv_title);
        ivBack = findViewById(R.id.iv_back);
        ivRightIcon = findViewById(R.id.iv_right_icon);
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.CustomTitle);

        int titleSize = a.getDimensionPixelSize(R.styleable.CustomTitle_titleSize,18);
        int titleColor = a.getColor(R.styleable.CustomTitle_titleColor,getResources().getColor(R.color.cfff));
        String title = a.getString(R.styleable.CustomTitle_title);
        tvTitle.setTextColor(titleColor);
        tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleSize);
        tvTitle.setText(title);

        //设置右边图标
        ivRightIcon.setOnClickListener(new OnClickEvent() {
            @Override
            public void singleClick(View v) {
                if(onIconListener!=null){
                    onIconListener.onClick();
                }
            }
        });
        //图标
        boolean isShow = a.getBoolean(R.styleable.CustomTitle_showIcon,false);
        if(isShow){
            ivRightIcon.setVisibility(VISIBLE);
            Drawable icon = a.getDrawable(R.styleable.CustomTitle_rightIcon);
            if(icon!=null){
                ivRightIcon.setImageDrawable(icon);
            }else{
                ivRightIcon.setImageResource(R.mipmap.wenjian);
            }
        }else{
            ivRightIcon.setVisibility(GONE);
        }

        a.recycle();
        ivBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onBackListener!=null){
                    onBackListener.onBack();
                }
            }
        });
    }

    public void setTitle(String title){
        tvTitle.setText(title);
    }

    private OnBackListener onBackListener;
    public void setOnBackListener(OnBackListener onBackListener){
        this.onBackListener = onBackListener;
    }
    public interface OnBackListener{
        void onBack();
    }

    private OnIconListener onIconListener;
    public void setOnIconListener(OnIconListener onIconListener) {
        this.onIconListener = onIconListener;
    }

    public interface OnIconListener{
        void onClick();
    }
}
